package com.scaffold.client.impl;

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.stats.CacheStats;
import com.scaffold.client.CacheClient;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.Map;

/**
 * Caffeine缓存客户端(本地缓存)
 * @author Da Shuai
 * @since 2021-08-30 16:32:45
 */
@Component
@RequiredArgsConstructor
public class CaffeineCacheClient implements CacheClient {

    private final Cache<String, Object> caffeineCache;

    @Override
    public void put(String k, Object v) {
        caffeineCache.put(k, v);
    }

    @Override
    public void remove(String k) {
        caffeineCache.asMap().remove(k);
    }

    @Override
    public Object get(String k) {
        return caffeineCache.asMap().get(k);
    }

    @Override
    public Collection<String> listKey(){
        return caffeineCache.asMap().keySet();
    }

    @Override
    public Map<String,Object> map(){
        return caffeineCache.asMap();
    }

    public String getStats(){
        return caffeineCache.stats().toString();
    }
}
